package com.sk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.entity.Orders;
import com.sk.mapper.OrdersMapper;
import com.sk.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-28
 */
@Service
public class OrdersServiceImpl  implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    public Result getByUserId(Integer userId) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("buyer_id", userId).or().eq("seller_id", userId);

        List<Orders> orderList = ordersMapper.selectList(wrapper);

        if (orderList != null && !orderList.isEmpty()) {
            return Result.getSuccess(orderList);
        }
        return Result.getFail("没有记录");
    }


    @Override
    public Result save(Orders order) {
        if (order.getOrderCreateTime() == null) {
            order.setOrderCreateTime(LocalDateTime.now());
        }
        int rows = ordersMapper.insert(order);
        if(rows > 0)
            return Result.saveSuccess(order);
        return Result.saveFail(order);
    }

}
